package com.eden.data.service;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.eden.data.mapper.*;
import com.eden.data.model.*;
import eden.api.common.dto.InviteMemberDto;
import eden.api.common.enums.InviteTypeEnum;
import eden.api.common.enums.MemberStatusEnum;
import eden.api.common.utils.ErrorEnums;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
@RequiredArgsConstructor
public class InviteMemberService {

    private final EdenPlatAgentInviteMapper edenPlatAgentInviteMapper;

    private final EdenAgentProfitParamMapper edenAgentProfitParamMapper;

    private final EdenMemberMapper edenMemberMapper;

    private final EdenMemberAccountMapper edenMemberAccountMapper;

    private final EdenPlatAgentInviteRecordMapper edenPlatAgentInviteRecordMapper;

    /**
     * 邀请会员服务
     * @param memberDto
     */
    @Transactional(rollbackFor = Exception.class)
    public void inviteMember(InviteMemberDto memberDto){
        // 1.查询是会员邀请还是代理商邀请
        InviteTypeEnum inviteTypeEnum = InviteTypeEnum.getEnum(memberDto.getInviteType());
        if(inviteTypeEnum == null){
            throw new IllegalArgumentException(ErrorEnums.ILLEGAL_PARAMETER.name());
        }
        // 如果是代理商邀请
        if(inviteTypeEnum.equals(InviteTypeEnum.AGENT)){
            // 代理商邀请
            // 通过邀请码获取代理商信息
            EdenPlatAgentInvite edenPlatAgentInvite = edenPlatAgentInviteMapper.selectListByInviteCode(memberDto.getPlatId(), memberDto.getInviteCode());
            if(edenPlatAgentInvite == null){
                throw new IllegalArgumentException(ErrorEnums.ILLEGAL_PARAMETER.name());
            }
            // 1.查询对应代理商设置的分润 eden_agent_profit_param
            List<EdenAgentProfitParam> edenAgentProfitParams = Optional.ofNullable(edenAgentProfitParamMapper.selectListByPlatId(memberDto.getPlatId())).orElse(new ArrayList<>(0));
            // 进行分润

            // 创建会员
            addMember(edenPlatAgentInvite.getPlatId(),edenPlatAgentInvite.getAgentId(),memberDto.getMemberName(),memberDto.getNickName(),null,  1);
            // 生成邀请记录
            addInviteRecord(edenPlatAgentInvite);

        }else{

        }

        // 2.如果是会员邀请走会员邀请流程

        // 会员邀请需要给会员分配金额


        //3 如果是代理商邀请走代理商邀请流程

        // 代理商邀请需要给代理商分配金额


        // 新增会员，并且建立账户

        // 生成邀请奖励账户

        //


    }

    private void addInviteRecord(EdenPlatAgentInvite edenPlatAgentInvite) {
        LocalDateTime now = LocalDateTime.now();
        EdenPlatAgentInviteRecord inviteRecord = new EdenPlatAgentInviteRecord();
        inviteRecord.setId(IdWorker.getId());
        inviteRecord.setPlatId(edenPlatAgentInvite.getPlatId());
        inviteRecord.setAgentId(edenPlatAgentInvite.getAgentId());
        inviteRecord.setInviteCode(edenPlatAgentInvite.getInviteCode());
        inviteRecord.setInviteTime(now);
        inviteRecord.setCreateTime(now);
        inviteRecord.setUpdateTime(now);
        edenPlatAgentInviteRecordMapper.insert(inviteRecord);
    }

    /**
     * 增加会员
     * @param platId 平台id
     * @param agentId 代理商id
     * @param memberName 会员名称
     * @param nickName 会员昵称
     * @param parentMemberId 上级会员id
     */
    private void addMember(Long platId, Long agentId, String memberName, String nickName, Long parentMemberId,Integer source) {
        LocalDateTime now = LocalDateTime.now();
        EdenMember edenMember = new EdenMember();
        edenMember.setId(IdWorker.getId());
        edenMember.setPlatId(platId);
        edenMember.setAgentId(agentId);
        edenMember.setMemberName(memberName);
        edenMember.setNickName(nickName);
        edenMember.setParentId(parentMemberId);
        edenMember.setStatus(MemberStatusEnum.REGISTERED.getCode());
        edenMember.setSource(source);
        edenMember.setRoutingPath(parentMemberId == null ? edenMember.getId().toString() : parentMemberId + "|" + edenMember.getId());
        edenMember.setCreateTime(now);
        edenMember.setUpdateTime(now);
        edenMemberMapper.insert(edenMember);

        // 新增会员账户
        EdenMemberAccount account = new EdenMemberAccount();
        account.setId(IdWorker.getId());
        account.setMemberId(edenMember.getId());
        account.setPlatId(platId);
        account.setAgentId(agentId);
        account.setAvailableAmount(BigDecimal.ZERO);
        account.setTotalAmount(BigDecimal.ZERO);
        account.setFrozenAmount(BigDecimal.ZERO);
        account.setCreateTime(now);
        account.setUpdateTime(now);
        edenMemberAccountMapper.insert(account);
    }

}
